package com.cml.dm.admin.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 元数据：任务标识、节点、表名、主键字段、下一个执行节点、备份sql、恢复sql、版本
 */
@Data
@TableName("dm_metadata")
public class DmMetadata {
    @TableId(type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private String first;

    @TableField(value="`task_ID`")
    private String taskID;

    private String nodeId;

    @TableField(value="`table`")
    private String table;

    private String pk;

    /**
     * 关联上一个执行节点的id作为主键
     */
    private  String joinUpPkNode;
    //关联的哪个任务id
    private String joinNodeId;

    private String interceptor;

    private String orgId;

    private String next;

    @TableField(exist = false)
    @JsonIgnore
    private  DmMetadata nextDmMetadata;

    @TableField(exist = false)
    private  DmMetadata upDmMetadata;

    /**
     * 主键值用${pk}进行占位
     */
    private String backupSql;

    private String recoverySql;

    @TableField(value="`version`")
    private Double version;

    private String sortField;

    @TableField(value="`limit`")
    private  Integer limit;

    private Integer threadNum;//线程数
    private Integer lazy;;
    @TableField(exist = false)
    @JsonIgnore
    private DmMetadata joinNode;

    private String shardingColumn;

    public void visitor(DmMetadata dmMetadata,List<DmMetadata> dmMetadataList){
        for (DmMetadata dm:dmMetadataList) {
            if(StringUtils.isEmpty(dmMetadata.getNext())){
                return;
            }
            if(dmMetadata.getNext().equals(dm.getNodeId())){
                dm.setUpDmMetadata(dmMetadata);
                dmMetadata.setNextDmMetadata(dm);
                this.visitor(dm,dmMetadataList);
            }
        }
    }
}
